jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.table.topServiceFaults.TopServiceFaults", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        	plugin.prototype.screenId;
			plugin.prototype.nameSuffix = "";
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.setUpComponent = function(nameSuffix, screenId) {
            	this.screenId = screenId;
				this.nameSuffix = nameSuffix;
				var tableLabel = this.getDescendantOfName("topFaultsTableLabel");
				tableLabel.setName(tableLabel.getName() + nameSuffix);
				var topServiceFaultsTable = this.getDescendantOfName("topServiceFaultsTable");
				topServiceFaultsTable.setName(topServiceFaultsTable.getName() + nameSuffix);
				
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.selectedTimeframeSubject, this.timeframeSelected, this);
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.environmentChangeSubject, this.environmentSelected, this);
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.general.NavigationHandler.mainPageDisplayTopic, this.handleDisplayScreen, this);
	            this.callgetTopServiceFaults();
            }
            
            plugin.prototype.timeframeSelected = function(subject, message, tableObj) {
            	var timeframeId = message.selectedTimeframe;
            	tableObj.callgetTopServiceFaults(timeframeId, null);
            }
            
            plugin.prototype.environmentSelected = function(subject, message, tableObj) {
            	var envId = message.environmentId;
            	tableObj.callgetTopServiceFaults(null, envId);
            }
            
            plugin.prototype.handleDisplayScreen = function(subject, message, tableObj) {
            	if(message.screenId == tableObj.screenId)
            	{ tableObj.updateComponent(); }
            }
            
            plugin.prototype.updateComponent = function() {
				this.callgetTopServiceFaults();
			}
			
			plugin.prototype.callgetTopServiceFaults = function(timeframeId, environmentId) {
				var server = tibco.admin.Main.getServer();
				
				if (server.getEnv("MODE")) {
                    // mode is live
                    var objService = tibco.admin.Main.loadService(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/topServiceFaults/rules/topServiceFaultsTableRules.xml", "getTopServiceFaults");
                    objService.setEndpointURL(plugin.wsEndpointURL);
		    		// this setInboundURL should not be required...only here because of a current AMX framework bug
		    		objService.setInboundURL("tibco/admin/monitormanage/uicomponents/table/topServiceFaults/data/topServiceFaultsTableData.xml");
                }
                else {
                    var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/topServiceFaults/rules/topServiceFaultsTableRules.xml"), "getTopServiceFaults", null, null);
                    // mode is static
                    objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/topServiceFaults/data/topServiceFaultsTableData.xml"));
                }
                
                objService.topServiceFaultsObj = this;
                if(!timeframeId)
				{ timeframeId = tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.getCurrentTimeframeId(); }
				if(!environmentId)
				{ environmentId = tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.getCurrentEnvironmentId(); }
				objService.timeframeId = timeframeId;
				objService.environmentId = environmentId;
                
                // initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("status");
				metricsList.add("faultCount");
				objService.metricsList = metricsList;

				//subscribe and call
				objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetTopServiceFaultsSuccess);
				objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetTopServiceFaultsError);
				objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetTopServiceFaultsInvalid);
				objService.doCall();
		    };

		    plugin.prototype.ongetTopServiceFaultsSuccess = function(objEvent) {
   				var topServiceFaultsObj = objEvent.target.topServiceFaultsObj;
				var tableObj = topServiceFaultsObj.getDescendantOfName("topServiceFaultsTable" + topServiceFaultsObj.nameSuffix);
				tableObj.repaintData();
		    };

		    plugin.prototype.ongetTopServiceFaultsError = function(objEvent) {
				var myStatus = objEvent.target.getRequest().getStatus();
				jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
		    };

		    plugin.prototype.ongetTopServiceFaultsInvalid = function(objEvent) {
				jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
		    };
			
			plugin.showServiceDetail = function(serviceIdVal) {
        		var paramArray = { serviceId:serviceIdVal };
        		tibco.admin.monitormanage.general.NavigationHandler.publishMainPageNavigationMessage(tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceScreenId, paramArray); 
        	}
        	
        });